﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Tile_Engine;

namespace AGrandVenture
{
    static class GoalManager
    {
        #region Declarations
        private static List<Generator> generators = new List<Generator>();
        private static int activeCount = 0;

        private static int minDistanceFromPlayer = 250;

        private static Random rand = new Random();

        private static Texture2D texture;
        private static Rectangle initialActiveFrame;
        private static Rectangle initialDisabledFrame;
        private static int activeFrameCount;
        private static int disabledFrameCount;
        #endregion

        #region Properties
        public static int ActiveGenerators
        {
            get { return activeCount; }
        }
        #endregion

        #region Initialization
        public static void Initialise(Texture2D textureSheet, Rectangle initialActiveRectangle, Rectangle initialDisabledRectangle, int activeFrames, int disabledFrames)
        {
            texture = textureSheet;
            initialActiveFrame = initialActiveRectangle;
            initialDisabledFrame = initialDisabledRectangle;
            activeFrameCount = activeFrames;
            disabledFrameCount = disabledFrames;
        }
        #endregion

        #region Generator Management
        public static Generator GeneratorInSquare(Vector2 mapLocation)
        {
            foreach (Generator generator in generators)
            {
                if (generator.MapLocation == mapLocation)
                {
                    return generator;
                }
            }
            return null;
        }

        public static void DetectShutDowns()
        {
            foreach (Generator generator in generators)
            {
                if (generator.Active)
                {
                    if (generator.IsCircleColliding(Player.BaseSprite.WorldCentre, Player.BaseSprite.CollisionRadius))
                    {
                        generator.Deactivate();
                        activeCount--;
                        GameManager.XP += 100;
                        MessageWindow.AddMessage("Enemy Lair Destroyed!  You have gained 100 XP! ", Color.RoyalBlue);
                    }
                }
            }
        }

        public static void AddGenerator()
        {
            int startX = rand.Next(2, TileMap.MapWidth - 2);
            int startY = rand.Next(0, TileMap.MapHeight - 2);

            Vector2 tileLocation = new Vector2(startX, startY);

            if ((GeneratorInSquare(tileLocation) != null) || (!TileMap.CellIsPassable(tileLocation)))
            {
                return;
            }

            if (Vector2.Distance(TileMap.GetCellCentre(startX, startY), Player.BaseSprite.WorldCentre) < minDistanceFromPlayer)
            {
                return;
            }

            List<Vector2> path = PathFinder.FindPath(new Vector2(startX, startY), TileMap.GetCellByPixel(Player.BaseSprite.WorldCentre));

            if (path != null)
            {
                Rectangle squareRect = TileMap.CellWorldRectangle(startX, startY);

                Sprite activeSprite = new Sprite(
                    new Vector2(squareRect.X, squareRect.Y),
                    texture,
                    initialActiveFrame,
                    Vector2.Zero,
                    0.85f);

                

                for (int x = 1; x < activeFrameCount + 1; x++)
                {
                    activeSprite.AddFrame(
                        new Rectangle(
                            initialActiveFrame.X + (x * initialActiveFrame.Width), initialActiveFrame.Y, initialActiveFrame.Width, initialActiveFrame.Height), 0.0f, false, false);
                }
                activeSprite.CollisionRadius = 15;

                Sprite disabledSprite = new Sprite(
                    new Vector2(squareRect.X, squareRect.Y),
                    texture,
                    initialDisabledFrame,
                    Vector2.Zero,
                    0.85f);

                Generator generator = new Generator(activeSprite, disabledSprite, new Vector2(startX, startY));

                float timerOffset = (float)rand.Next(1, 100);
                generator.LastSpawnCounter = timerOffset / 100f;

                generators.Add(generator);

                activeCount++;
            }
        }

        public static void AddGenerator(Vector2 location)
        {
            int startX = (int)location.X;
            int startY = (int)location.Y;

            Vector2 tileLocation = new Vector2(startX, startY);

            if ((GeneratorInSquare(tileLocation) != null) || (!TileMap.CellIsPassable(tileLocation)))
            {
                return;
            }

            if (Vector2.Distance(TileMap.GetCellCentre(startX, startY), Player.BaseSprite.WorldCentre) < minDistanceFromPlayer)
            {
                return;
            }

            List<Vector2> path = PathFinder.FindPath(new Vector2(startX, startY), TileMap.GetCellByPixel(Player.BaseSprite.WorldCentre));

            if (path != null)
            {
                Rectangle squareRect = TileMap.CellWorldRectangle(startX, startY);

                Sprite activeSprite = new Sprite(
                    new Vector2(squareRect.X, squareRect.Y),
                    texture,
                    initialActiveFrame,
                    Vector2.Zero,
                    0.85f);



                for (int x = 1; x < activeFrameCount + 1; x++)
                {
                    activeSprite.AddFrame(
                        new Rectangle(
                            initialActiveFrame.X + (x * initialActiveFrame.Width), initialActiveFrame.Y, initialActiveFrame.Width, initialActiveFrame.Height), 0.0f, false, false);
                }
                activeSprite.CollisionRadius = 15;

                Sprite disabledSprite = new Sprite(
                    new Vector2(squareRect.X, squareRect.Y),
                    texture,
                    initialDisabledFrame,
                    Vector2.Zero,
                    0.85f);

                Generator generator = new Generator(activeSprite, disabledSprite, new Vector2(startX, startY));

                float timerOffset = (float)rand.Next(1, 100);
                generator.LastSpawnCounter = timerOffset / 100f;

                generators.Add(generator);

                activeCount++;
            }
        }

        public static void AddGenerator(int x, int y)
        {
            Vector2 location = new Vector2(x, y);
            AddGenerator(location);
        }

        #endregion

        #region Public Methods
        public static void GenerateEnemyGenerators(int generatorCount)
        {
            generators.Clear();
            activeCount = 0;

            while (activeCount < generatorCount)
            {
                AddGenerator();
            }
            activeCount = 11;
        }

        public static void Update(GameTime gameTime)
        {
            DetectShutDowns();
            foreach (Generator generator in generators)
            {
                generator.Update(gameTime);
            }
        }

        public static void Draw(SpriteBatch spriteBatch)
        {
            foreach (Generator generator in generators)
            {
                generator.Draw(spriteBatch);
            }
        }
        #endregion


    }
}
